package id.net.iconpln.bb.client.view;

import id.net.iconpln.bb.client.data.TestData;
import id.net.iconpln.bb.client.model.PageBbAntriBean;
import id.net.iconpln.bb.client.model.PageBbBongkarBean;
import id.net.iconpln.bb.client.model.PageBbKdtgKapalBean;
import id.net.iconpln.bb.client.model.PageBbSandarBean;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.CheckBoxGroup;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Element;

public class PageBbSandar extends LayoutContainer {
	Grid<PageBbSandarBean> grid;
	Grid<PageBbAntriBean> grid1;
	private FormData formData;
	private FormBinding formBindings; 
	
	@Override
	  protected void onRender(Element parent, int pos) {
	    super.onRender(parent, pos); 
	    setLayout(new FitLayout());  
	    
	    formData = new FormData("-20");
	    ContentPanel cp = new ContentPanel();  
	  
	    cp.setHeading("Proses Perizinan Sandar");  
	    cp.setFrame(true);  
	    cp.setSize(1030, 600);  
	    cp.setLayout(new RowLayout(Orientation.HORIZONTAL));  
	  
	    grid1 = createGrid1();	   
	    grid1.setBorders(false);
	    grid1.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
	    grid1.getSelectionModel().addListener(Events.SelectionChange,  
	            new Listener<SelectionChangedEvent<PageBbKdtgKapalBean>>() {  
	              public void handleEvent(SelectionChangedEvent<PageBbKdtgKapalBean> be) {  
	                if (be.getSelection().size() > 0) {  
	                  formBindings.bind((ModelData) be.getSelection().get(0));  
	                } else {  
	                  formBindings.unbind();  
	                }  
	              }  
	            });  
	    grid = createGrid();
	    grid.setBorders(false);
	    grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
	  
	    ContentPanel pan = new ContentPanel();
	    pan.setHeaderVisible(false);
	    pan.setBorders(false);
	    pan.setLayout(new RowLayout(Orientation.VERTICAL));
	    
	    ContentPanel pan11 = new ContentPanel();
	    pan11.setBorders(true);
	    pan11.setHeading("Daftar Sandar");
	    pan11.setLayout(new RowLayout(Orientation.VERTICAL));
	    
	    pan11.add(grid,new RowData(1, 1));
	    
	    ContentPanel pan12 = new ContentPanel();
//	    pan12.setHeaderVisible(false);
	    pan12.setBorders(true);	    
	    pan12.setHeading("Daftar Antri");
	    pan12.setLayout(new RowLayout(Orientation.VERTICAL));
	    
	    pan12.add(grid1,new RowData(1, 1));
	    
	    pan.add(pan12, new RowData(1, .5));
	    pan.add(pan11, new RowData(1, .5));
	    
	    FormPanel panel = createForm();
	    formBindings = new FormBinding(panel, true);
	  
	    cp.add(panel, new RowData(.4, 1));  
	    cp.add(pan, new RowData(.6, 1)); 
	    add(cp, new FitData(5)); 
	  }  
	  
	  private FormPanel createForm() {  
	    FormPanel panel = new FormPanel();  
	    panel.setHeaderVisible(false);
	    
	    FieldSet fieldSet2 = new FieldSet();  
	    fieldSet2.setHeading("Persetujuan Sandar");  
	    fieldSet2.setCollapsible(false);

	    FormLayout layout2 = new FormLayout();  
	    layout2.setLabelWidth(150);  
	    fieldSet2.setLayout(layout2);
	    
	    final SimpleComboBox<String> txtconfirm = new SimpleComboBox<String>();  
	    txtconfirm.setFieldLabel("Persetujuan");  
	    txtconfirm.setForceSelection(true);  
	    txtconfirm.setTypeAhead(true);   
	    txtconfirm.setTriggerAction(TriggerAction.ALL);
	    txtconfirm.add("Confirm");
	    txtconfirm.add("Pending");
	    txtconfirm.add("Reject");
	    fieldSet2.add(txtconfirm, formData);
	    
	    final TextField<String> txtnama1 = new TextField<String>();
	    txtnama1.setName("nama_transport");
	    txtnama1.setFieldLabel("Nama Transport");  
	    fieldSet2.add(txtnama1, formData);
	    
	    final SimpleComboBox<String> txtjenis = new SimpleComboBox<String>();  
	    txtjenis.setFieldLabel("Jenis Transport");  
	    txtjenis.setForceSelection(true);  
	    txtjenis.setTypeAhead(true);   
	    txtjenis.setTriggerAction(TriggerAction.ALL);
	    txtjenis.add("Kapal");
	    txtjenis.add("Tongkang");
	    txtjenis.add("Truck");
	    txtjenis.add("Conveyor");
	    fieldSet2.add(txtjenis, formData); 
	    
	    final SimpleComboBox<String> txtdermaga1 = new SimpleComboBox<String>();
	    txtdermaga1.setFieldLabel("Dermaga");
	    txtdermaga1.setForceSelection(true);  
	    txtdermaga1.setTypeAhead(true);   
	    txtdermaga1.setTriggerAction(TriggerAction.ALL);
	    txtdermaga1.add("DM01");
	    txtdermaga1.add("DM02");
	    txtdermaga1.add("DM03");
	    fieldSet2.add(txtdermaga1, formData);
	    
	    final TextField<String> txtpengiriman = new TextField<String>();  
	    txtpengiriman.setFieldLabel("No Pengiriman");  
	    fieldSet2.add(txtpengiriman, formData);
	    
	    final TextField<String> txtkontrak = new TextField<String>();  
	    txtkontrak.setFieldLabel("No Kontrak");  
	    fieldSet2.add(txtkontrak, formData);
	    
	    DateTimeFormat dtFormat = DateTimeFormat.getFormat("d/M/yy h:mma");
	    final DateField txtwkt_sandar = new DateField();  
	    txtwkt_sandar.setFieldLabel("Tanggal Sandar");
	    txtwkt_sandar.getPropertyEditor().setFormat(dtFormat);
	    fieldSet2.add(txtwkt_sandar, formData);
	    
	    final TextField<String> txtvol = new TextField<String>();  
	    txtvol.setFieldLabel("Volume(Ton)");  
	    fieldSet2.add(txtvol, formData);
	    
	    final TextField<String> txtstatus = new TextField<String>();  
	    txtstatus.setFieldLabel("Status");  
	    fieldSet2.add(txtstatus, formData);
	    
	    panel.add(fieldSet2);
	    
	    ContentPanel p = new ContentPanel();
	    p.setHeaderVisible(false);	    
	    p.setButtonAlign(HorizontalAlignment.RIGHT);  
	    p.addButton(new Button("Reset", new SelectionListener<ButtonEvent>() {  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	        grid.getStore().rejectChanges();
	        txtnama1.setValue("");
	        txtjenis.setValue(null);
	        txtdermaga1.setValue(null);
	        txtwkt_sandar.setValue(null);
	        txtvol.setValue("");
	        txtstatus.setValue("");
	      }  
	    }));  
	    p.addButton(new Button("Submit", new SelectionListener<ButtonEvent>() {  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	        grid.getStore().commitChanges();
	        MessageBox.alert("Message", "DATABASE sedang dalam Perbaikan", null);
	      }  
	    }));  

	    panel.add(p);
	    return panel;  
	  }  
	  
	  private Grid<PageBbSandarBean> createGrid() {  
	    
	   
	    final List<ColumnConfig> columns = new ArrayList<ColumnConfig>(); 
	    
	    RowNumberer rn = new RowNumberer();  
	    rn.setWidth(30);  
	    
	    ColumnConfig nama = new ColumnConfig("nama_transport", "Nama Transport", 300);
	    nama.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig dokumen = new ColumnConfig("jenis_transport", "Jenis Transport", 110);
	    dokumen.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig pemasok = new ColumnConfig("no_pengiriman", "No Pengiriman", 100);
	    pemasok.setAlignment(HorizontalAlignment.RIGHT);
	    
	    ColumnConfig dermaga = new ColumnConfig("no_kontrak", "No Kontrak", 102);
	    dermaga.setAlignment(HorizontalAlignment.RIGHT);
	    
	    ColumnConfig waktu_sandar = new ColumnConfig("tgl_tiba", "Tanggal Tiba", 102);
	    waktu_sandar.setAlignment(HorizontalAlignment.LEFT);
	    waktu_sandar.setDateTimeFormat(DateTimeFormat.getShortDateTimeFormat());
	    
	    ColumnConfig start_bongkar = new ColumnConfig("volume", "Volume", 102);
	    start_bongkar.setAlignment(HorizontalAlignment.RIGHT);
	    
	    ColumnConfig end_bongkar = new ColumnConfig("status", "Status", 102);
	    end_bongkar.setAlignment(HorizontalAlignment.LEFT);
	    
	    columns.add(rn);
	    columns.add(nama);
	    columns.add(dokumen);
	    columns.add(pemasok);
	    columns.add(dermaga);
	    columns.add(waktu_sandar);
	    columns.add(start_bongkar);
	    columns.add(end_bongkar);
	   
	    ListStore<PageBbSandarBean> store = new ListStore<PageBbSandarBean>();  
	    store.setMonitorChanges(true);  
	    store.add(TestData.getDMSandar());  
	  
	    final ColumnModel cm = new ColumnModel(columns);
	    
	    grid = new Grid<PageBbSandarBean>(store, cm);
	    grid.setBorders(true);  
	    
	    return grid;  
	  }  
	  
	  private Grid<PageBbAntriBean> createGrid1() {  
		    
		   
		    final List<ColumnConfig> columns = new ArrayList<ColumnConfig>(); 
		    
		    RowNumberer rn = new RowNumberer();  
		    rn.setWidth(30);  
		    
		    ColumnConfig nama = new ColumnConfig("nama_transport", "Nama Transport", 363);
		    nama.setAlignment(HorizontalAlignment.LEFT);
		    
		    ColumnConfig mulai_antri = new ColumnConfig("tanggal_antri", "Tanggal Antri", 110);
		    mulai_antri.setAlignment(HorizontalAlignment.LEFT);
		    mulai_antri.setDateTimeFormat(DateTimeFormat.getShortDateTimeFormat());
		    
		    ColumnConfig tgl = new ColumnConfig("dermaga", "Dermaga", 102);
		    tgl.setAlignment(HorizontalAlignment.LEFT);
		    
		    columns.add(rn);
		    columns.add(nama);
		    columns.add(tgl);
		    columns.add(mulai_antri);
		   
		    ListStore<PageBbAntriBean> store = new ListStore<PageBbAntriBean>();  
		    store.setMonitorChanges(true);  
		    store.add(TestData.getDMAntri());  
		  
		    final ColumnModel cm = new ColumnModel(columns);
		    
		    grid1 = new Grid<PageBbAntriBean>(store, cm);
		    grid1.setBorders(true);  
		    
		    return grid1;  
		  }
}

